package com.example.day9;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.day9.Model.Bean;
import com.example.day9.Presener.NetPresener;
import com.example.day9.View.NetView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private Toolbar main_toolbar;
    private RecyclerView main_recycler;
    private NavigationView main_navigation;
    private DrawerLayout main_drawer;
    private ArrayList<Bean.ResultsBean> list;
    private RAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        main_toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        main_recycler = (RecyclerView) findViewById(R.id.main_recycler);
        main_navigation = (NavigationView) findViewById(R.id.main_navigation);
        main_drawer = (DrawerLayout) findViewById(R.id.main_drawer);

        setSupportActionBar(main_toolbar);

        ActionBarDrawerToggle abdt = new ActionBarDrawerToggle(this, main_drawer, main_toolbar, R.string.open, R.string.close);
        abdt.syncState();

        list = new ArrayList<>();


        adapter = new RAdapter(this, list);
        main_recycler.setLayoutManager(new LinearLayoutManager(this));
        main_recycler.setAdapter(adapter);


        initData();

    }

    private void initData() {
        NetPresener netPresener = new NetPresener();

        netPresener.initData(new NetView() {
            @Override
            public void getData(Bean bean) {
                adapter.addData(bean.getResults());
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 1, 1, "线性布局");
        menu.add(0, 2, 2, "网络布局");
        menu.add(0, 3, 3, "瀑布布局");

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                //线性布局
                main_recycler.setLayoutManager(new LinearLayoutManager(this));
                notify();
                break;
            case 2:
                //网络布局
                main_recycler.setLayoutManager(new GridLayoutManager(this, 2));
                notify();
                break;
            case 3:
                //瀑布布局
                main_recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                notify();
                break;

        }


        return super.onOptionsItemSelected(item);
    }
}
